package com.webside.bvideo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.webside.base.basecontroller.BaseController;
import com.webside.dtgrid.model.Pager;
import com.webside.dtgrid.util.ExportUtils;
import com.webside.exception.AjaxException;
import com.webside.exception.SystemException;
import com.webside.series.model.BvideoEntity;
import com.webside.series.model.SeriesEntity;
import com.webside.series.service.BvideoService;
import com.webside.series.service.SeriesService;
import com.webside.util.Common;
import com.webside.util.PageUtil;
import com.webside.videoresource.model.VideoResourceEntity;
import com.webside.videoresource.service.VideoResourceService;

@Controller
@Scope("prototype")
@RequestMapping("/bvideo/")
public class BvideoController extends BaseController {
  
	@Autowired
	private BvideoService bvideoService;
	@Autowired
	private VideoResourceService videoResourceService;
	@Autowired
	private SeriesService seriesService;
	
	@RequestMapping("listUI.html")
	public String listUI(Model model, HttpServletRequest request, Long id,Long seriesId) {
		 
		try
		{ 
			 
			PageUtil page = new PageUtil();
			if(request.getParameterMap().containsKey("page")){
				page.setPageNum(Integer.valueOf(request.getParameter("page")));
				page.setPageSize(Integer.valueOf(request.getParameter("rows")));
				page.setOrderByColumn(request.getParameter("sidx"));
				page.setOrderByType(request.getParameter("sord"));
			}
			model.addAttribute("page", page);
			
			if(seriesId!=null && seriesId!=0){
				model.addAttribute("seriesId", seriesId);
			}else if(id!=null && id!=0){
				model.addAttribute("seriesId", id);
			}
			
			return Common.BACKGROUND_PATH + "/bvideo/list";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	
	@RequestMapping("list.html")
	@ResponseBody
	public Object list(String gridPager,HttpServletRequest request,HttpServletResponse response) throws Exception{
		 
		String seriesId = request.getParameter("seriesId");
		/*reques.getAttribute(bvideo.getSeriesId().toString());*/
		Map<String,Object> parameters = null;
		// 映射Pager对象
		Pager pager = JSON.parseObject(gridPager, Pager.class);
		// 判断是否包含自定义参数
		parameters = pager.getParameters();
		parameters.put("seriesId",seriesId);
		if (parameters.size() < 0) {
			parameters.put("title", null);
			System.out.println(parameters);
		}//3、判断是否是导出操作
		
		if(pager.getIsExport())
		{
			if(pager.getExportAllData())
			{
				//3.1、导出全部数据
				List<BvideoEntity> list = bvideoService.queryListByPage(parameters);
				for (BvideoEntity bvideoEntity : list) {
					System.out.println(bvideoEntity.getTitle());
					System.out.println(bvideoEntity.getSetNum());
				}
				System.out.println(list);
				ExportUtils.exportAll(response, pager, list);
				return null;
			}else
			{
				//3.2、导出当前页数据
				ExportUtils.export(response, pager);
				return null;
			}
		}else
		{
		// 设置分页，page里面包含了分页信息
		Page<Object> page = PageHelper.startPage(pager.getNowPage(),pager.getPageSize());
	
		List<BvideoEntity> list = bvideoService.queryListByPage(parameters);
         	
		parameters.clear();
		parameters.put("isSuccess", Boolean.TRUE);
		parameters.put("nowPage", pager.getNowPage());
		parameters.put("pageSize", pager.getPageSize());
		parameters.put("pageCount", page.getPages());
		parameters.put("recordCount", page.getTotal());
		parameters.put("startRecord", page.getStartRow());
		//列表展示数据
		parameters.put("exhibitDatas", list);
		return parameters;
	}
	}
	
	@RequestMapping("addUI.html")
	public String addUI(Model model,Long seriesId) {
		
		System.out.println(seriesId);
		model.addAttribute("seriesId", seriesId);
		return Common.BACKGROUND_PATH + "/bvideo/form";
	}
 
	//添加
	@RequestMapping("add.html")
	@ResponseBody
	public Object add(Model model,BvideoEntity bvideo,SeriesEntity series,Long seriesId,HttpServletRequest request,HttpServletResponse response)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		try
		{ 
			int result = bvideoService.insert(bvideo);
			if(result>0)
			{
				map.put("success", Boolean.TRUE);
				map.put("data", null);
				map.put("message", "添加成功");
				series.setId(new Long(seriesId).intValue());
				int update=seriesService.updateSetSum(series);
				if(update>0){
					map.put("success", Boolean.TRUE);
					map.put("data", null);
				}
			}else
			{
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "添加失败");
			}
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		return map;
	} 
	
	
	//修改显示
		@RequestMapping("editUI.html")
		public String editUI(Model model, HttpServletRequest request,Long seriesId,String resourceName, Long id) {
			try
			{
				System.out.println(seriesId);
				model.addAttribute("seriesId", seriesId);
			    model.addAttribute("resourceName", resourceName);
			 
				BvideoEntity bvideoenty=bvideoService.findById(id);
				PageUtil page = new PageUtil();
				page.setPageNum(Integer.valueOf(request.getParameter("page")));
				page.setPageSize(Integer.valueOf(request.getParameter("rows")));
				page.setOrderByColumn(request.getParameter("sidx"));
				page.setOrderByType(request.getParameter("sord"));
			
				model.addAttribute("page", page);
				
				model.addAttribute("bvideoentity", bvideoenty);

				return Common.BACKGROUND_PATH + "/bvideo/form";
			}catch(Exception e)
			{
				throw new SystemException(e);
			}
		}
		//修改
		@RequestMapping("edit.html")
		@ResponseBody
		public Object update(BvideoEntity bvideoIds )
		{
			Map<String, Object> map = new HashMap<String, Object>();
			try
			 {  	 
					int result = bvideoService.update(bvideoIds);
					System.out.println(result);
				if(result >0)
				{
					map.put("success", Boolean.TRUE);
					map.put("data", null);
					map.put("message", "编辑成功");
					
				}else
				{
					map.put("success", Boolean.FALSE);
					map.put("data", null);
					map.put("message", "编辑失败");
				}
			}catch(Exception e)
			{
				throw new AjaxException(e);
			}
			return map;
		}
		
		//删除
		@RequestMapping("deleteBatch.html")
		@ResponseBody
		public Object deleteBatch(String ids) {
			Map<String, Object> result = new HashMap<String, Object>();
			try
			{
				String[] seriesIds = ids.split(",");
				List<Long> list = new ArrayList<Long>();
				for (String string : seriesIds) {
					list.add(Long.valueOf(string));
				}
				int cnt = bvideoService.deleteBatchById(list);
				System.out.println(cnt);
				if(cnt == list.size())
				{
					result.put("success", true);
					result.put("data", null);
					result.put("message", "删除成功");
				}else
				{
					result.put("success", false);
					result.put("data", null);
					result.put("message", "删除失败");
				}
			}catch(Exception e)
			{
				throw new AjaxException(e);
			}
			return result;
		}
		
		
		
		
	@RequestMapping("resources.html")
	public String resources(Model model) {
		List<VideoResourceEntity> list = videoResourceService.selectall(-1,null);
		int count=list.size();
		PageUtil pageUtil =new PageUtil();
		pageUtil.setPageNum(0);
		pageUtil.setPageSize(15);
		List<VideoResourceEntity> list1=videoResourceService.findListByName(pageUtil,"");
		model.addAttribute("videoResource_list", list1);
		int totalPageCount;
		if(count>15 ){
			totalPageCount=count/15;
			 if(count%15==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		
		System.out.println("剧集数："+count);
		return Common.BACKGROUND_PATH + "/bvideo/resources";
	}
	
	@RequestMapping(value="/searchPage.html", method={RequestMethod.GET,RequestMethod.POST})
	public String searchPage(Model model,VideoResourceEntity bSeries, String pSize,String pageNo,String sort,String name){
		PageUtil pageUtil =new PageUtil();
		int no=Integer.parseInt(pageNo)-1;
		pageUtil.setPageNum(no);
		pageUtil.setPageSize(Integer.parseInt(pSize));
		System.out.println(sort);
		int totalPageCount;
		int count=videoResourceService.selectall(-1,sort).size();
		if(count>15 ){
			totalPageCount=count/15;
			 if(count%15==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		if(sort==null){
			sort="";
		}
		List<VideoResourceEntity> list=videoResourceService.findListByName(pageUtil,sort);
		model.addAttribute("videoResource_list", list);
		System.out.println(totalPageCount);
		return Common.BACKGROUND_PATH+"/bvideo/resourceslist";
	}
}
